package com.crx.springbootinit.model.dto.page;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.formula.functions.T;

import java.util.Collections;
import java.util.List;

/**
 * @author 谢杭延
 * @Date 2024-10-31 13:19
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("统一分页返回结果")
public class PageVO<T> {

    @ApiModelProperty("总条数")
    private Long total;
    @ApiModelProperty("总页数")
    private Long pages;
    @ApiModelProperty("返回列表")
    private List<T> list;

    public static <PO,VO> PageVO<VO> of(Page<PO> p,Class<VO> clazz){
        PageVO<VO> vo = new PageVO<>();
        vo.setPages(p.getPages());
        vo.setTotal(p.getTotal());
        List<PO> records=p.getRecords();
        if(CollUtil.isEmpty(records)){
            vo.setList(Collections.emptyList());
            return vo;
        }
        List<VO> voList = BeanUtil.copyToList(records, clazz);
        vo.setList(voList);
        return vo;
    }


}


